package com.hjm.project.agentInfo.distributionAdjust.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hjm.project.agentInfo.distributionAdjust.mapper.DistributionAdjustMapper;
import com.hjm.project.agentInfo.distributionAdjust.domain.DistributionAdjust;
import com.hjm.project.agentInfo.distributionAdjust.service.IDistributionAdjustService;
import com.hjm.common.utils.text.Convert;
import org.springframework.web.multipart.MultipartFile;

/**
 * 服务商分润调整项Service业务层处理
 * 
 * @author generator
 * @date 2020-11-27
 */
@Service
public class DistributionAdjustServiceImpl implements IDistributionAdjustService 
{
    @Autowired
    private DistributionAdjustMapper distributionAdjustMapper;

    /**
     * 查询服务商分润调整项
     * 
     * @param id 服务商分润调整项ID
     * @return 服务商分润调整项
     */
    @Override
    public DistributionAdjust selectDistributionAdjustById(Long id)
    {
        return distributionAdjustMapper.selectDistributionAdjustById(id);
    }

    /**
     * 查询服务商分润调整项列表
     * 
     * @param distributionAdjust 服务商分润调整项
     * @return 服务商分润调整项
     */
    @Override
    public List<DistributionAdjust> selectDistributionAdjustList(DistributionAdjust distributionAdjust)
    {
        return distributionAdjustMapper.selectDistributionAdjustList(distributionAdjust);
    }

    /**
     * 新增服务商分润调整项
     * 
     * @param distributionAdjust 服务商分润调整项
     * @return 结果
     */
    @Override
    public int insertDistributionAdjust(DistributionAdjust distributionAdjust)
    {
        return distributionAdjustMapper.insertDistributionAdjust(distributionAdjust);
    }

    /**
     * 修改服务商分润调整项
     * 
     * @param distributionAdjust 服务商分润调整项
     * @return 结果
     */
    @Override
    public int updateDistributionAdjust(DistributionAdjust distributionAdjust)
    {
        return distributionAdjustMapper.updateDistributionAdjust(distributionAdjust);
    }

    /**
     * 删除服务商分润调整项对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteDistributionAdjustByIds(String ids)
    {
        return distributionAdjustMapper.deleteDistributionAdjustByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除服务商分润调整项信息
     * 
     * @param id 服务商分润调整项ID
     * @return 结果
     */
    @Override
    public int deleteDistributionAdjustById(Long id)
    {
        return distributionAdjustMapper.deleteDistributionAdjustById(id);
    }

    /**
     * 导入分润调整项
     * @param file 文件
     * @param loginName 登录人
     * @return 结果
     */
    @Override
    public int uploadDistributionAdjust(MultipartFile file, String loginName){
        //file
        InputStream inputStream = null;
        try {
            ExcelUtil<DistributionAdjust> util = new ExcelUtil<>(DistributionAdjust.class);
            inputStream = file.getInputStream();
            List<DistributionAdjust> distributionAdjustList = util.importExcel(inputStream);
            if (distributionAdjustList.size() > 0){
                //存储添加人信息
                for (DistributionAdjust adjust : distributionAdjustList) {
                    adjust.setAddMan(loginName);
                    adjust.setAddTime(DateUtils.getDateTime());
                }
                return distributionAdjustMapper.batchInsertDistributionAdjust(distributionAdjustList);
            }
        }catch (Exception e){

        }finally {
            try {
                if (inputStream != null){
                    inputStream.close();
                }
            } catch (IOException e) {}
        }
        return 0;
    }

    @Override
    public int examine(DistributionAdjust distributionAdjust) {
        return distributionAdjustMapper.examine(distributionAdjust);
    }
}
